package com.ehotting.mqbd.core.manager;

import com.ehotting.eaf.core.StringHelper;
import com.ehotting.eaf.core.utils.NumberHelper;
import com.ehotting.eaf.redis.service.RedisService;
import com.ehotting.mqbd.api.MQDBConstants;
import com.ehotting.mqbd.api.bean.ConsumerNodeBean;
import com.ehotting.mqbd.core.service.AgentRefChannelService;
import com.ehotting.mqbd.core.service.AgentRefClientService;
import com.ehotting.mqbd.core.service.ConsumerNodeService;
import com.ehotting.mqbd.core.service.AgentRefConsumerService;
import com.ehotting.mqbd.core.util.AgentIdGenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class AgentLinkManager {

    private static Logger logger = LoggerFactory.getLogger(AgentLinkManager.class);

    @Autowired
    private ConsumerNodeService consumerNodeService;
    @Autowired
    private AgentRefConsumerService agentRefConsumerService;
    @Autowired
    private AgentRefChannelService agentRefChannelService;
    @Autowired
    private AgentRefClientService agentRefClientService;

    public String getAgentIdByChannelId(String module,String channelId){
        return agentRefChannelService.getAgentIdByChannelId(module, channelId);
    }

    public void deleteAgentIdByChannelId(String module,String channelId){
        agentRefChannelService.deleteAgentIdByChannelId(module, channelId);
    }

    public void setAgentIdByChannelId(String module,String channelId,String agentId){
        agentRefChannelService.setAgentIdByChannelId( module, channelId, agentId);
    }


    public void setClientIpAndUserIdAndProjectId(String module, Long userId, String projectId, String clientIp){
        agentRefClientService.setClientIpAndUserIdAndProjectId( module,  userId,  projectId,  clientIp);
    }

    public void setClientIpAndAgentId(String agentId, String clientIp){
        agentRefClientService.setClientIpAndAgentId( agentId,  clientIp);
    }

    public void deleteClientIpAndAgentId(String agentId){
        agentRefClientService.deleteClientIpAndAgentId( agentId);
    }


    public String getClientIpByChannelId(String module, String channelId){
        String agentId = agentRefChannelService.getAgentIdByChannelId(module,channelId);
        logger.info("Get agentId:{}",agentId);
        if(StringHelper.isBlank(agentId)){
            logger.info("Get agentId:{} by channelId:{},return null.",agentId,channelId);
            return null;
        }
        return agentRefChannelService.getClientIpByAgentId(agentId);
    }
    public String getClientIpByAgentId( String agentId){
        return agentRefChannelService.getClientIpByAgentId(agentId);
    }


    public String genAgentId(String module,Long userId,String projectId){
        return AgentIdGenUtil.genAgentId(module, userId,projectId);
    }


    public String getConsumerIpAndAgentId(String module,String agentId){

        if(StringHelper.isBlank(agentId)){
            agentId= module+":0-0";
        }

        String consumerIp = null;
        String tempConsumerIp = agentRefConsumerService.getConsumerIpByAgentId( agentId);
        if(StringHelper.hasText(tempConsumerIp)){
            /**增加判断consumerIp**/
            ConsumerNodeBean consumerNode = consumerNodeService.get(tempConsumerIp);
            if(consumerNode!=null){
                consumerIp = consumerNode.getIp();
            }else{
                agentRefConsumerService.deleteConsumerIpByAgentId(agentId);
            }

        }

        if(consumerIp==null) {
            ConsumerNodeBean consumerNode = consumerNodeService.getLive();
            if (consumerNode != null) {
                consumerIp = consumerNode.getIp();
                agentRefConsumerService.setConsumerIpAndAgentId(agentId, consumerIp);
            }
        }
        logger.info("Get consumerIp[{}] by  key[{}].",consumerIp,agentId);
        return consumerIp;
    }

    public String genDefaultAgentId(String module){
        return AgentIdGenUtil.genDefaultAgentId(module);
    }

    public String getModule(String agentId){
        return  AgentIdGenUtil.getModule(agentId);
    }



    public Long getUserId(String agentId){
        return  AgentIdGenUtil.getUserId(agentId);
    }

    public Long getProjectId(String agentId){
        return  AgentIdGenUtil.getProjectId(agentId);
    }

}